package com.lmqb.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.lmqb.bean.JPhoneSubject;
import com.lmqb.bean.JRefuseReason;
import com.lmqb.bean.LmAgentApply;
import com.lmqb.bean.LmRepay;
import com.lmqb.bean.LmSchedule;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.SysBlack;
import com.lmqb.bean.SysConfig;
import com.lmqb.dao.JCheckArtificialMapper;
import com.lmqb.dao.JPhoneSubjectMapper;
import com.lmqb.dao.JRefuseReasonMapper;
import com.lmqb.dao.LmLoanMapper;
import com.lmqb.dao.LmRepayMapper;
import com.lmqb.dao.LmScheduleMapper;
import com.lmqb.dao.LmUserMapper;
import com.lmqb.dao.SysConfigMapper;
import com.lmqb.entry.Config;
import com.lmqb.entry.DataCheck;
import com.lmqb.entry.PassRate;
import com.lmqb.page.PageTool;
import com.lmqb.page.SchedulePager;
import com.lmqb.service.SysBaseService;
import com.lmqb.util.RedisUtil;
import com.lmqb.util.StrUtil;

@Service("sysConfigService")
public class SysBaseServiceImpl implements SysBaseService{

	@Autowired
	private SysConfigMapper sysConfigDao;
	
	@Autowired
	private LmUserMapper lmUserDao;
	
	@Autowired
	private LmScheduleMapper scheduleDao;
	
	@Autowired
	private JRefuseReasonMapper refuseDao;
	
	@Autowired
	private JPhoneSubjectMapper phoneSubjectDao;
	
	@Autowired
	private JCheckArtificialMapper checkDao;
	
	@Autowired
	private LmLoanMapper loanDao;
	
	@Autowired
	private LmRepayMapper rePayDao;
	
	//基础配置列表
	public List<SysConfig> selectAllSysConfig() {
		List<SysConfig> list=sysConfigDao.selectAll();
		return list;
	}

	//@Override
	public SchedulePager selectSchedulePager(SchedulePager pagerModel) {
		List<LmSchedule> list=scheduleDao.selectAll(pagerModel);
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
			list.get(i).setHandelTimeD(StrUtil.intToTime(list.get(i).getHandelTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoginMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = scheduleDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	//基础配置
	public int updateConfig(Config config) {
		int updateSysConfigPm = sysConfigDao.updateSysConfigPm(config);
		System.out.println("修改成功");
		List<SysConfig> list = sysConfigDao.selectAllCfg();
		String groub="params_config";
		String key[]=new String[list.size()];
		String value[]=new String[list.size()];
		//实例化一个jedis对象，连接到指定的服务器，指定连接端口号
        Jedis jedis = null;
        jedis=RedisUtil.initPool().getResource();
        for(int i=0;i<list.size();i++){
        key[i]=list.get(i).getPmCate()+"_"+list.get(i).getPmKey();
        value[i]=list.get(i).getPmValue();
        jedis.hset(groub,key[i],value[i]);
        }
        //关闭连接
        jedis.close();
		return updateSysConfigPm;
	}

	//@Override
	public List<JRefuseReason> selectRefuses(JRefuseReason reason) {
		List<JRefuseReason> list=refuseDao.selectAll(reason);
		return list;
	}

	//@Override
	public List<JPhoneSubject> selectAllPhone(JPhoneSubject phoneSubject) {
		List<JPhoneSubject> list=phoneSubjectDao.selectAll(phoneSubject);
		return list;
	}

	/*
	 * 统计数据通过率
	 * */
	public PassRate selectPassRate() {
		PassRate passrate=new PassRate();
		DataCheck dataCheck=new DataCheck();
		dataCheck.setIsToday(1);
		dataCheck.setType((byte)1);
		//今日申请人数。今天申请材料审核的人数
		passrate.setUserNumToday(checkDao.selectApplyNum(dataCheck));
		dataCheck.setIsToday(0);
		//总申请人数
		passrate.setApplyNum(checkDao.selectApplyNum(dataCheck));
		dataCheck.setIsToday(1);
		
		dataCheck.setStatus((byte)2);
		//今天材料审核失败的人数
		passrate.setCailiaoNotPassNum(checkDao.selectApplyNum(dataCheck));
		dataCheck.setStatus((byte)1);
		//今天材料审核通过的人数
		passrate.setCailiaoPassNum(checkDao.selectApplyNum(dataCheck));
		Date today=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String str=sdf.format(today);
		Integer todayInt=StrUtil.strToInt(str);
		//今日放款人数（实际借款）
		passrate.setFangkuanTodayNum(loanDao.selectFangKuanNum(todayInt));
		dataCheck.setType((byte)3);
		//今日应放款总金额（人工审核通过的总额度）
		passrate.setFangkuanTotal(checkDao.selectSumCredit(dataCheck));
		
		//今日待放款金额（人工审核通过的人数-今日放款人数）
		passrate.setDaiTodayNum(checkDao.selectApplyNum(dataCheck)-passrate.getFangkuanTodayNum());
		//机器审核未通过人数。
		passrate.setJiqiNotPassNum(0);
		//机器神审核通过人数。即申请审核的人数
		passrate.setJiqiPassNum(passrate.getApplyNum());
		todayInt=null;
		//累计已放款金额（实际借款）
		passrate.setYifangTotal(loanDao.selectFangKuan(todayInt));
		dataCheck.setIsToday(0);
		//累计待放款金额（
		passrate.setDaiTotal(checkDao.selectSumCredit(dataCheck).subtract(passrate.getYifangTotal()));
		return passrate;
	}
	//还款数据查询
	public PassRate selectRePay(){
		PassRate passrate=new PassRate();
		DataCheck dataCheck=new DataCheck();
		//提前还款的总金额
		dataCheck.setIsToday(1);
		dataCheck.setStatus((byte)3);
		BigDecimal selectRepayToday = rePayDao.selectRepayToday(dataCheck);
		//还款成功
		dataCheck.setStatus((byte)2);
		BigDecimal selectRepayToday1 = rePayDao.selectRepayToday(dataCheck);
		if(selectRepayToday!= null){
			BigDecimal add = selectRepayToday.add(selectRepayToday1);//两个数的和
			passrate.setRepayToday(add);//今日还款金额
		}
		
		//总金额不是今天的
		dataCheck.setIsToday(0);
		//待还款
		dataCheck.setStatus((byte)0);
		BigDecimal selectRepayToday2 = rePayDao.selectRepayToday(dataCheck);
		passrate.setRepayNo(selectRepayToday2);
		
		//总还款金额
		dataCheck.setStatus((byte)3);
		BigDecimal selectRepayToday3 = rePayDao.selectRepayToday(dataCheck);
		//还款成功
		dataCheck.setStatus((byte)2);
		BigDecimal selectRepayToday4 = rePayDao.selectRepayToday(dataCheck);
		if(selectRepayToday3 != null){
			BigDecimal addAll = selectRepayToday3.add(selectRepayToday4);//两个数的和
			passrate.setRepayTotal(addAll);//还款总金额
		}
		
		
		//逾期未还款金额
		List<LmRepay> selectAllOrder = rePayDao.selectAllOrder();
		//判断是否逾期
		for(int i=0;i<selectAllOrder.size();i++){
			if(selectAllOrder.get(i).getOverdueDays()>0){
				dataCheck.setStatus((byte)0);
			  BigDecimal selectRepayToday5 = rePayDao.selectRepayToday(dataCheck);
			  passrate.setRepayNoTimeOut(selectRepayToday5);
			}
		}
		return passrate;
		
		
	}

	//查询所有cfg_param的数据
	public List<SysConfig> selectAllCfg() {
		List<SysConfig> selectAllCfg = sysConfigDao.selectAllCfg();
		return selectAllCfg;
	}

}
